package com.cheyun.netsalev3.view

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyPagedListAdapter
import com.cheyun.netsalev3.bean.Data
import com.cheyun.netsalev3.bean.order.OrderKeliuData
import com.cheyun.netsalev3.databinding.ActivityChoiceCustomerBinding
import com.cheyun.netsalev3.databinding.ActivityChoiceOrderCustomerBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.utils.api.NetworkState
import com.cheyun.netsalev3.viewmodel.ChoiceOrderCustomerActivityViewModel

class ChoiceOrderCustomerActivity : BaseActivity<ActivityChoiceOrderCustomerBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_choice_order_customer
    }

    override fun initViewModel(): ViewModel? {

        var phone = intent.getStringExtra("phone")
        var rcvuid = intent.getStringExtra("rcvuid")
        var tid = ""
        if(intent.hasExtra("tid")) {
            tid = intent.getStringExtra("tid")
        }

        val factory = InjectorUtils.getChoiceOrderCustomerActivityViewModelFactory(phone,rcvuid,tid)
        return ViewModelProviders.of(this, factory).get(ChoiceOrderCustomerActivityViewModel::class.java)
    }

    private lateinit var adapter: MyPagedListAdapter<OrderKeliuData>

    override fun initView() {
        adapter = MyPagedListAdapter(R.layout.choice_order_customer_item, binding.viewModel)
//        adapter = MyPagedListAdapter(R.layout.fragment_item2, binding.viewModel)

        var recyclerView = binding.root.findViewById<RecyclerView>(R.id.swipe_target)
        recyclerView.adapter = adapter
        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.viewModel?.refresh()
        }
    }

    override fun initData() {
        binding.viewModel?.listdata?.observe(this, Observer { list ->
            adapter.submitList(list)
        })

        binding.viewModel?.networkState?.observe(this, Observer {
            binding.swipeRefreshLayout.isRefreshing=it== NetworkState.LOADING
            adapter.setNetworkState(it)
        })
    }


}
